/*
 * Created on 14 �.�. 2547
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.softplus.carrefour.hd.bltier.admin;

import java.util.List;
import java.util.Vector;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;

import com.softplus.carrefour.CFException;
import com.softplus.carrefour.hd.bltier.db.HibernateUtil;
import com.softplus.carrefour.hd.bltier.entity.Timezone;
import com.softplus.carrefour.hd.bltier.entity.UserOfficeHour;

/**
 * @author Kowit
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class TimeZoneManage implements ITimeZoneControl, ITimeZoneViewer {

    /* (non-Javadoc)
     * @see com.softplus.carrefour.hd.bltier.admin.ITimeZoneControl#addTimeZone(com.softplus.carrefour.hd.bltier.entity.Timezone)
     */

    public void addTimeZone(Timezone tz) throws CFException {
        HibernateUtil.insertEntity(tz);
    }

    /* (non-Javadoc)
     * @see com.softplus.carrefour.hd.bltier.admin.ITimeZoneControl#updateTimeZone(com.softplus.carrefour.hd.bltier.entity.Timezone)
     */

    public void updateTimeZone(Timezone tz) throws CFException {
        HibernateUtil.updateEntity(tz);
    }

    /* (non-Javadoc)
     * @see com.softplus.carrefour.hd.bltier.admin.ITimeZoneControl#deleteTimeZone(com.softplus.carrefour.hd.bltier.entity.Timezone)
     */

    public void deleteTimeZone(Timezone tz) throws CFException {
        HibernateUtil.deleteEntity(tz);
    }

    /* (non-Javadoc)
     * @see com.softplus.carrefour.hd.bltier.admin.ITimeZoneViewer#getAllTimeZone()
     */

    public Vector getAllTimeZone() throws CFException {

        String hql = null;
        hql = "from Timezone t ";

        List ls = null;
        try {
            ls = HibernateUtil.getSession().createQuery(hql).list();
        } catch (HibernateException e) {
            e.printStackTrace();
        } finally {
            HibernateUtil.closeSession();
        }
        return new Vector(ls);
    }

    /* (non-Javadoc)
     * @see com.softplus.carrefour.hd.bltier.admin.ITimeZoneViewer#getTimeZoneByCountry(java.lang.String)
     */

    public Timezone getTimeZoneByCountry(String countryId) throws CFException {
        Timezone tz = null;
        try {
            tz = 
 (Timezone)HibernateUtil.getSession().get(Timezone.class, countryId);
        } catch (HibernateException e) {
            e.printStackTrace();
        } finally {
            HibernateUtil.closeSession();
        }
        return tz;
    }

    /* (non-Javadoc)
     * @see com.softplus.carrefour.hd.bltier.admin.ITimeZoneViewer#getOrigin()
     */

    public Timezone getOrigin() throws CFException {

        Timezone tz = null;
        String hql = null;
        hql = "from Timezone t where t.originCountry = 'Y'";

        List ls = null;
        try {
            ls = HibernateUtil.getSession().createQuery(hql).list();
        } catch (HibernateException e) {
            e.printStackTrace();
        } finally {
            HibernateUtil.closeSession();
        }
        if (ls.size() == 1)
            tz = (Timezone)ls.get(0);

        return tz;
    }

    /* (non-Javadoc)
     * @see com.softplus.carrefour.hd.bltier.admin.ITimeZoneViewer#getNotCountryTimeZone(java.lang.String)
     */

    public Vector getNotCountryTimeZone(String countryId) throws CFException {

        String hql = null;
        hql = "from Timezone t where t.countryId != '" + countryId + "'";

        List ls = null;
        try {
            ls = HibernateUtil.getSession().createQuery(hql).list();
        } catch (HibernateException e) {
            e.printStackTrace();
        } finally {
            HibernateUtil.closeSession();
        }
        return new Vector(ls);
    }
}
